<?php

class VenueController extends Zend_Controller_Action {

    public function init() {
        $this->view->headTitle('MusicEvents')->headTitle('Venue')->setSeparator(' :: ');
        $this->view->accordion = 2;
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('search', 'html')
                ->addActionContext('venue', 'html')
                ->addActionContext('venue2', 'json')
                ->addActionContext('events', 'json')
                ->initContext();
    }

    public function validateAction() {
        if ($this->getRequest()->isPost()) {
            $form = new Application_Form_Venue();
            if ($this->getRequest()->isPost()) {
                $formData = $this->getRequest()->getPost();
                if ($form->isValid($formData)) {
                    $this->getRequest()->setDispatched();
                    $filter = new Zend_Filter_StripTags();
                    $venue = $filter->filter($form->getValue('venue'));
                    $country = $filter->filter($form->getValue('country'));
                    if ($country != "all")
                        $this->_redirect('venue/search/' . urlencode($venue) . '/' . urlencode($country));
                    else
                        $this->_redirect('venue/search/' . urlencode($venue));
                } else {
                    $form->populate($formData);
                    $this->view->venueForm = $form;
                }
            }
        }
    }

    public function searchAction() {
        if ($this->getRequest()->has('venue')) {
            $venue = $this->_getParam('venue');
            $country = ($this->getRequest()->has('country')) ? $this->_getParam('country') : null;
            $page = $this->_getParam('page', 1);
            $itemCountPerPage = 8;
            $paginator = new Zend_Paginator(new Application_Model_Venue($venue, $itemCountPerPage, $page, $country));
            $paginator->setItemCountPerPage($itemCountPerPage);
            $paginator->setCurrentPageNumber($page);
            $this->view->searchPattern = $venue;
            $this->view->matches = $paginator->getTotalItemCount();
            $this->view->pages = ceil($paginator->getTotalItemCount() / $paginator->getItemCountPerPage());
            $this->view->paginator = $paginator;
            $this->view->headTitle('Search for '. $venue);
            $form = new Application_Form_Venue();
            $form->populate(array('venue' => $venue, 'country' => $country));
            $this->view->venueForm = $form;
        }
    }

    public function showAction() {
        if ($this->getRequest()->has('id') && $this->getRequest()->has('venue')) {
            $id = urlencode($this->_getParam('id'));
            $venue = urlencode($this->_getParam('venue'));
            Lastfm_Venue::isValid($id);
            $this->view->venue = urldecode($venue);
            $this->view->venue_ajax = '/venue/venue/id/' . $id;
            $this->view->event_json = '/venue/events/id/' . $id;
            $this->view->event_tmp = '/template/venue';
            $response = $this->getResponse();
            $response->insert('sidecontent', $this->view->render('venue/side.phtml'));
        }
    }

    public function venueAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $id = $this->_getParam('id');
            $this->view->venue = Lastfm_Venue::getInfo($id);
        }
    }

    public function venue2Action() {
        if ($this->_request->isXmlHttpRequest()) {
            $id = $this->_getParam('id');
            $venue = Lastfm_Venue::getInfo($id);
            echo json_encode(Application_Model_Event::venueToArray($venue));
            exit;
        }
    }

    public function eventsAction() {
        if ($this->_request->isXmlHttpRequest()) {
            $id = $this->_getParam('id');
            $events = Lastfm_Venue::getEvents($id);
            echo json_encode(Application_Model_Event::toArray($events));
            exit;
        }
    }

}

